//
//  CALayer+SJAdd.swift
//  SJSwiftKit
//
//  Created by swordjoy on 2017/3/9.
//  Copyright © 2017年 swordjoy. All rights reserved.
//

import Foundation
import UIKit

public extension SwordJoy where Base: CALayer {
  public var left : CGFloat {
    get { return base.frame.origin.x }
    set { base.frame.origin.x = newValue }
  }
  
  public var top : CGFloat {
    get { return base.frame.origin.y }
    set { base.frame.origin.y = newValue }
  }
  
  public var right : CGFloat {
    get { return base.frame.origin.x + base.frame.size.width }
    set { base.frame.origin.x = newValue - base.frame.size.width }
  }
  
  public var bottom : CGFloat {
    get { return base.frame.origin.y + base.frame.size.height }
    set { base.frame.origin.y = newValue - base.frame.size.height }
  }
  
  public var height : CGFloat {
    get { return base.frame.size.height }
    set { base.frame.size.height = newValue }
  }
  
  public var width : CGFloat {
    get { return base.frame.size.width }
    set { base.frame.size.width = newValue }
  }
  
  public var center: CGPoint {
    get { return CGPoint(x: base.frame.origin.x + base.frame.size.width * 0.5, y: base.frame.origin.y + base.frame.size.height * 0.5) }
    set {
      base.frame.origin.x = newValue.x - base.frame.size.width * 0.5;
      base.frame.origin.y = newValue.y - base.frame.size.height * 0.5
    }
  }
  
  public var centerX : CGFloat {
    get { return self.center.x }
    set { self.center = CGPoint(x: newValue, y: self.center.y) }
  }
  
  public var centerY : CGFloat {
    get { return self.center.y }
    set { self.center = CGPoint(x: self.center.x, y: newValue) }
  }
  
  public var size: CGSize {
    get { return base.frame.size }
    set { base.frame.size = newValue }
  }

  
  public func mediaTimingFunction(from curve: UIViewAnimationCurve) -> String {
    var mediaFunction: String = ""
    switch curve {
    case .easeInOut:
      mediaFunction = kCAMediaTimingFunctionEaseInEaseOut
    case .easeIn:
      mediaFunction = kCAMediaTimingFunctionEaseIn
    case .easeOut:
      mediaFunction = kCAMediaTimingFunctionEaseOut
    case .linear:
      mediaFunction = kCAMediaTimingFunctionLinear
    }
    return mediaFunction
  }
  
  public func fade(_ duration: TimeInterval, curve: UIViewAnimationCurve) {
    let mediaFunction = mediaTimingFunction(from: curve)
    
    let transition = CATransition()
    transition.duration = duration
    transition.timingFunction = CAMediaTimingFunction(name: mediaFunction)
    transition.type = kCATransitionFade
    base.add(transition, forKey: "sjkit.fade")
  }
  
  public func removeFade() {
    base.removeAnimation(forKey: "sjkit.fade")
  }
  
  public func pushAnimation(_ duration: TimeInterval, direction: SJDirection = .left, curve: UIViewAnimationCurve = .easeInOut){
    let mediaFunction = mediaTimingFunction(from: curve)
    let transition = CATransition()
    transition.duration = duration
    transition.timingFunction = CAMediaTimingFunction(name: mediaFunction)
    transition.type = kCATransitionPush
    
    switch direction {
    case .left:
      transition.subtype = kCATransitionFromLeft
    case .right:
      transition.subtype = kCATransitionFromRight
    case .bottom:
      transition.subtype = kCATransitionFromBottom
    }
    
    base.add(transition, forKey: "sjkit.push")
  }
}

public extension SwordJoy where Base: CALayer {
  
  var transformScale: CGFloat {
    get {
      let v: NSNumber = base.value(forKeyPath: "transform.scale") as! NSNumber
      return v.cgFloat
    }
    set {
      base.setValue(newValue, forKeyPath: "transform.scale")
    }
  }
  
}

public enum SJDirection {
  case left
  case right
  case bottom
}
